package com.lijian.controller;

import com.lijian.base.PageTableRequest;
import com.lijian.base.Result;
import com.lijian.dto.RoleDto;
import com.lijian.model.SysRole;
import com.lijian.service.RoleService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/all")
    @ApiOperation(value = "获取所有角色", notes = "获取所有角色信息")//描述
    @PreAuthorize("hasAnyAuthority('sys:role:query')")
    @ResponseBody
    public Result<SysRole> findAll(){
        return roleService.findAllRoles();
    }

    @GetMapping("/list")
    @ApiOperation(value = "分页获取角色", notes = "用户分页获取角色信息")//描述
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "limit", required = true,dataType = "Integer"),
    })
    @PreAuthorize("hasAnyAuthority('sys:role:query')")
    @ResponseBody
    public Result list(PageTableRequest request){
        request.countOffset();
        return roleService.findAllRolesByPage(request.getOffset(), request.getLimit());
    }

    @GetMapping("/findRoleBySearch")
    @PreAuthorize("hasAnyAuthority('sys:role:query')")
    @ApiOperation(value = "模糊查询角色信息", notes = "模糊查询角色信息")//描述
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName",value = "模糊搜索的角色", required = true),
    })
    @ResponseBody
    public Result findRoleBySearch(PageTableRequest request, String roleName){
        request.countOffset();
        return roleService.findRoleBySearch(roleName, request.getOffset(), request.getLimit());
    }

    @GetMapping("/add")
    @ApiOperation(value = "新增角色信息页面", notes = "跳转到角色信息新增页面")//描述
    @PreAuthorize("hasAnyAuthority('sys:role:add')")
    public String addRole(Model model){
        model.addAttribute(new SysRole());
        return "role/role-add";
    }

    @PostMapping("/add")
    @ApiOperation(value = "保存角色信息", notes = "保存新增的角色信息")//描述
    @ApiImplicitParam(name = "roleDto",value = "角色信息实体类", required = true,dataType = "RoleDto")
    @ResponseBody
    public Result save(@RequestBody RoleDto roleDto){
        return roleService.save(roleDto);
    }

    @GetMapping("/edit")
    @ApiOperation(value = "编辑角色信息页面", notes = "跳转到角色信息编辑页面")//描述
    @ApiImplicitParam(name = "roleId",value = "角色id", required = true)
    @PreAuthorize("hasAnyAuthority('sys:role:edit')")
    public String editRole(Model model, @RequestParam("id") Integer roleId){
        SysRole roleById = roleService.findRoleById(roleId);
        model.addAttribute("sysRole", roleById);
        return "role/role-edit";
    }

    @PostMapping("/edit")
    @PreAuthorize("hasAnyAuthority('sys:role:edit')")
    @ApiOperation(value = "保存角色信息", notes = "保存被编辑的角色信息")//描述
    @ApiImplicitParam(name = "roleDto",value = "角色信息实体类", required = true,dataType = "RoleDto")
    @ResponseBody
    public Result<SysRole> updateRole(@RequestBody RoleDto roleDto){
        return roleService.update(roleDto);
    }

    @GetMapping("/delete")
    @PreAuthorize("hasAnyAuthority('sys:role:del')")
    @ApiOperation(value = "删除角色信息", notes = "删除角色信息")//描述
    @ResponseBody
    public Result delete(RoleDto roleDto){
        return roleService.delete(roleDto.getId());
    }
}

